<template>
  <div class="mod-faceDesign">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item label="设计稿名称:">
        <el-input
          v-model="dataForm.designName"
          placeholder="设计稿名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="设计稿状态:">
        <el-select
          v-model="dataForm.designStatus"
          clearable
          placeholder="请选择设计稿状态"
        >
          <el-option
            v-for="item in designStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否公开:">
        <el-select
          v-model="dataForm.isPublic"
          clearable
          placeholder="请选择是否公开"
        >
          <el-option
            v-for="item in isPublicOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getDataList()">
          查询
        </el-button>
        <el-button @click="clearSearch()"> 清空 </el-button>
      </el-form-item>
    </el-form>

    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @selection-change="selectionChange"
      @on-load="getDataList"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          size="small"
          >批量删除</el-button
        >
      </template>

      <template slot="designStatus" slot-scope="scope">
        <el-tag :type="getStatusTagType(scope.row.designStatus)">
          {{ getStatusLabel(scope.row.designStatus) }}
        </el-tag>
      </template>

      <template slot="isPublic" slot-scope="scope">
        <el-tag :type="scope.row.isPublic === 1 ? 'success' : 'info'">
          {{ scope.row.isPublic === 1 ? "公开" : "私有" }}
        </el-tag>
      </template>

      <template #menu="scope">
        <el-button
          type="text"
          size="small"
          @click="addOrUpdateHandle(scope.row.designId)"
          >修改</el-button
        >
        <el-button
          type="text"
          size="small"
          @click="deleteHandle(scope.row.designId)"
          >删除</el-button
        >
        <el-button type="text" size="small" @click="statusHandle(scope.row)"
          >状态</el-button
        >
      </template>
    </avue-crud>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>

  <!-- 分页 -->
  <el-pagination
    :current-page="page.currentPage"
    :page-sizes="[10, 20, 50, 100]"
    :page-size="page.pageSize"
    :total="page.total"
    layout="total, sizes, prev, pager, next, jumper"
    @size-change="sizeChangeHandle"
    @current-change="currentChangeHandle"
  />
</template>

<script>
import AddOrUpdate from "./add-or-update.vue";
import { tableOption } from "@/crud/prod/faceDesign.js";

export default {
  data() {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      tableOption: tableOption,
      dataForm: {}, // 搜索表单数据
      // 设计稿状态选项
      designStatusOptions: [
        { value: 1, label: "正常" },
        { value: 2, label: "禁用" },
        { value: 3, label: "删除" },
      ],
      // 是否公开选项
      isPublicOptions: [
        { value: 1, label: "公开" },
        { value: 0, label: "私有" },
      ],
    };
  },
  components: {
    AddOrUpdate,
  },
  methods: {
    // 获取数据列表
    getDataList(page, params, done) {
      this.dataListLoading = true;
      const pageParam = page == null ? this.page : page;
      http({
        url: http.adornUrl("/admin/faceDesign/page"),
        method: "get",
        params: http.adornParams(
          Object.assign(
            {
              current:
                pageParam == null
                  ? this.page.currentPage
                  : pageParam.currentPage,
              size: pageParam == null ? this.page.pageSize : pageParam.pageSize,
              designName: this.dataForm.designName,
              designStatus: this.dataForm.designStatus,
              isPublic: this.dataForm.isPublic,
            },
            params
          ),
          false
        ),
      }).then(({ data }) => {
        this.dataList = data.records;
        this.page.total = data.total;
        this.dataListLoading = false;
        if (done) done();
      });
    },
    // 多选
    selectionChange(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.designId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        http({
          url: http.adornUrl("/admin/faceDesign"),
          method: "delete",
          data: http.adornData(ids, false),
        }).then(({ data }) => {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
          });
          this.getDataList();
        });
      });
    },
    // 状态修改
    statusHandle(row) {
      this.$confirm(`确定对"${row.designName}"进行状态修改操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        http({
          url: http.adornUrl("/admin/faceDesign/status"),
          method: "put",
          data: http.adornData({
            designIds: [row.designId],
            designStatus: row.designStatus === 1 ? 2 : 1,
          }),
        }).then(({ data }) => {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
          });
          this.getDataList();
        });
      });
    },
    // 状态标签类型
    getStatusTagType(status) {
      const types = {
        1: "success",
        2: "warning",
        3: "danger",
      };
      return types[status] || "info";
    },
    // 状态标签文字
    getStatusLabel(status) {
      const labels = {
        1: "正常",
        2: "禁用",
        3: "删除",
      };
      return labels[status] || "未知";
    },
    // 搜索
    searchChange(params) {
      this.getDataList(this.page, params);
    },
    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.currentPage = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.currentPage = val;
      this.getDataList();
    },
    // 清空搜索
    clearSearch() {
      this.dataForm = {};
      this.getDataList();
    },
  },
};
</script>

<style lang="scss" scoped>
.mod-faceDesign {
  .el-form {
    background-color: #fff;
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }

  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style>
